// 本机登录
import React, {Component} from 'react';
import {
    Dimensions,
    Image,
    StyleSheet,
    TouchableOpacity,
    View,
    ScrollView,
    Text,
    Platform
} from 'react-native';
import StorageUtil           from '../utils/StorageUtil';
import LoadingView           from '../views/LoadingView';
import *as BaseAPI           from '../utils/BaseAPI';
import  DeviceInfo           from 'react-native-device-info';
import Toast, {DURATION}     from 'react-native-easy-toast';
import {NavigationActions}   from 'react-navigation';
import  LinearGradient       from 'react-native-linear-gradient';
import BaseNavigationBar     from '../views/BaseNavigationBar';
const {width} = Dimensions.get('window');


export default class LocalLoginScreen extends Component {
    constructor(props) {
        super(props);
        this.state = {
            showProgress:false
        };
    }

    goLogin(){
        
        var bundleID= DeviceInfo.getBundleId();
        var UniqueID= DeviceInfo.getUniqueID();
        var versionID= DeviceInfo.getVersion();
        var typeStr ;
        if (Platform.OS === 'ios'){
            typeStr = 'appstore';
        } else {
            typeStr = 'android';
        }

        var URL=BaseAPI.HostUrl+bundleID+'&duid='+UniqueID
            +'&gss=320&type='+typeStr+'&platform=mobile&ver='+versionID+'&lang=zh_CN';
        this.setState({showProgress:true});

        console.log('登录地址'+ URL);

        fetch(URL, {
            method:'GET'
        }).then((res)=>res.json())
            .then((json)=>{
                this.setState({showProgress:false});

                console.log('登录成功之后'+ JSON.stringify(json));

                if (json.state ===0){
                    this.refs.toast.show('登录成功');
                    StorageUtil.set('hasLogin',{'hasLogin':true});
                    StorageUtil.set('token',{'token':json.token});

                    StorageUtil.set('Config',{'Config':json.config});


                    this.timer = setTimeout(()=>{
                        // 跳到主界面
                        const resetAction = NavigationActions.reset({
                            index: 0,
                            actions: [
                                NavigationActions.navigate({routeName: 'Home'})
                            ]
                        });
                        this.props.navigation.dispatch(resetAction);
                    },1000)
                }else{
                    this.refs.toast.show('登录失败');
                }
            })
            .catch((e)=>{
                this.setState({showProgress:false});
                this.refs.toast.show('网络请求出错：' + e);
            })
    }

    popToBack(){
        this.props.navigation.goBack()
    }

    render() {
        return (
            <View style={styles.container}>

                {/*<BaseNavigationBar*/}
                    {/*title={'登录'}*/}
                    {/*leftImageSource={require('../../images/other/ic_back.png')}*/}
                    {/*leftItemFunc={() => this.popToBack()}/>*/}

                <ScrollView>

                    <View style={styles.loginViewStyle}>
                        <Image style={styles.logoStyle}
                               source={require('../../images/login/pic_logo.png')}/>
                    </View>

                    <TouchableOpacity activeOpacity={1} onPress={this.goLogin.bind(this)}>
                        <LinearGradient colors={['#fe7133','#ff4114']} style={styles.linearGradient}>
                            <Text style={{color:'#fdfdfd',fontSize:14}}>本机登录</Text>
                        </LinearGradient>
                    </TouchableOpacity>

                </ScrollView>

                <Toast ref="toast" position='center'/>
                { this.state.showProgress?(<LoadingView cancel={() => this.setState({showProgress:false})}/>):(null)}

            </View>
        );
    }
}

const styles = StyleSheet.create({
    container: {
        flex: 1,
        backgroundColor:'white'
    },
    logoStyle:{
        height:0.56*260,
        width:0.56*220
    },
    loginViewStyle:{
        marginTop:width/4,
        justifyContent:'center',
        alignItems:'center'
    },
    linearGradient: {
        justifyContent:'center',
        alignItems:'center',
        height:0.56*70,
        marginLeft:0.56*40,
        marginRight:0.56*40,
        marginTop:0.56*80,
        borderRadius:0.56*20
    },
});
